package com.example.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.utils.R;
import com.example.system.dto.RoleDTO;
import com.example.system.entity.Role;
import com.example.system.mapper.RoleMapper;
import com.example.system.service.IRoleService;
import com.example.system.service.IUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 王鹤蒲
 * @since 2023-08-13
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    private final static Logger log= LoggerFactory.getLogger(RoleServiceImpl.class);

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private IUserService iUserService;

    @Override
    public R selectAll(int currentPage, int pageSize, String name) {
        Page<Role> page=new Page<>(currentPage,pageSize);
        LambdaQueryWrapper<Role> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Role::getDelFlag,'0');
        lambdaQueryWrapper.like(StrUtil.isNotBlank(name),Role::getRoleName,name);
        roleMapper.selectPage(page,lambdaQueryWrapper);
        return R.success(page);
    }

    @Override
    public R save(RoleDTO roleDTO) {
        Role role=new Role();
        BeanUtil.copyProperties(roleDTO,role,true);
        LocalDateTime createTime= LocalDateTime.now();
        role.setCreateTime(createTime);
        role.setCreateBy(iUserService.getCurrentLoginUser().getUserName());
        roleMapper.insert(role);
        return R.success();
    }

    @Override
    public R deleteById(Integer id) {
        LambdaUpdateWrapper<Role> lambdaUpdateWrapper=new LambdaUpdateWrapper<Role>();
        lambdaUpdateWrapper.eq(Role::getRoleId,id);
        LocalDateTime deleteTime=LocalDateTime.now();
        roleMapper.update(new Role().setDelFlag("1").setUpdateBy(iUserService.getCurrentLoginUser().getUserName()).setUpdateTime(deleteTime),lambdaUpdateWrapper);
        return R.success();
    }

    @Override
    public R deleteByIds(Integer[] ids) {
        LambdaUpdateWrapper<Role> lambdaUpdateWrapper=new LambdaUpdateWrapper<Role>();
        LocalDateTime deleteTime=LocalDateTime.now();
        for(Integer id:ids){
            lambdaUpdateWrapper.eq(Role::getRoleId,id);
            roleMapper.update(new Role().setDelFlag("1").setUpdateBy(iUserService.getCurrentLoginUser().getUserName()).setUpdateTime(deleteTime),lambdaUpdateWrapper);
            lambdaUpdateWrapper.clear();
        }
        return R.success();
    }

    @Override
    public R getRoleByRoleId(Integer roleId){
           LambdaQueryWrapper<Role> lambdaQueryWrapper=new LambdaQueryWrapper<>();
           lambdaQueryWrapper.eq(Role::getRoleId,roleId);
           lambdaQueryWrapper.eq(Role::getDelFlag,'0');
          return R.success(roleMapper.selectOne(lambdaQueryWrapper));
    }

    @Override
    public R edit(Integer id,RoleDTO roleDTO) {
        Role role=new Role();
        BeanUtil.copyProperties(roleDTO,role,true);
        LocalDateTime updateTime=LocalDateTime.now();
        role.setRoleId(id).setUpdateBy(iUserService.getCurrentLoginUser().getUserName()).setUpdateTime(updateTime);
        roleMapper.updateById(role);
        return R.success();
    }

    @Override
    public R editRoleStatus(Integer id, Boolean status) {
        LambdaUpdateWrapper<Role> lambdaUpdateWrapper=new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(Role::getRoleId,id);
        lambdaUpdateWrapper.set(Role::getStatus,status);
        lambdaUpdateWrapper.set(Role::getUpdateBy,iUserService.getCurrentLoginUser().getUserName());
        LocalDateTime updateTime=LocalDateTime.now();
        lambdaUpdateWrapper.set(Role::getUpdateTime,updateTime);
        roleMapper.update(new Role(),lambdaUpdateWrapper);
        return R.success();
    }

}
